using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Tabs;
using DotNetNuke;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Common.Utilities;

using Common;
public partial class controls_tm_typicalProductLogoList : System.Web.UI.UserControl
{
    public int myPortalId = Null.NullInteger;
    public int ShowProductList(int ownerId)
    {
        try
        {
            string mySQLCmd =
            "SELECT productId,name1,description FROM tm_product " +
            " WHERE registerBy = " + ownerId; 
            return ShowProductList(mySQLCmd,Common.consts.constSystemPortalId);
        }
        catch (Exception exc)
        {
            Exceptions.ProcessModuleLoadException(this, exc);
        }
        return 0;
    }
    public int ShowProductList()
    {
        try
        {

            int numItem = Common.systemSettings.sysTypicalProductMaxRow * Common.systemSettings.sysTypicalProductPerRow;
            string mySQLCmd =
            "SELECT TOP " + numItem + " a.productId,a.name1,a.description " +
            " FROM tm_product a " +
            " WHERE a.grade='" + Common.consts.constTypicalGrade + "' AND " +
            " a.status='" + Common.consts.constStatusENABLE + "' AND expireDate > '" + sysLibs.ConvertToSQLDateString(DateTime.Now) + "'";
            return ShowProductList(mySQLCmd, Common.consts.constSystemPortalId);
        }
        catch (Exception exc)
        {
            Exceptions.ProcessModuleLoadException(this, exc);
        }
        return 0;
    }


    private int ShowProductList(string mySQLCmd,int portalId)
    {
        try
        {
            int tabId = Common.appLibs.GetTabIdByName(portalId, "product");
            ModuleInfo moduleInfo = Common.appLibs.GetModuleInfo(portalId, "product");
            string fileName;

            SqlConnection conn = new SqlConnection(sysLibs.GetConnectionString);
            SqlDataAdapter daTmp = new SqlDataAdapter(new SqlCommand(mySQLCmd, conn));
            DataTable dataTbl = new DataTable();
            daTmp.Fill(dataTbl);
            conn.Close();
            ArrayList dataArr = new ArrayList();
            for (int idx = 0; idx < dataTbl.Rows.Count; idx++)
            {
                controls_logoList.myImgLinkInfo imgInfo = new controls_logoList.myImgLinkInfo();
                imgInfo.linkUrl = DotNetNuke.Common.Globals.NavigateURL(tabId, "viewDetail", "mid=" + moduleInfo.ModuleID, "productId=" + dataTbl.Rows[idx]["productId"].ToString());

                fileName = GetFileName(int.Parse(dataTbl.Rows[idx]["productId"].ToString()));
                if (fileName != "")
                {
                    string filePathName = sysLibs.GetUpLoadProductDir + "/" + sysLibs.GetHashString(fileName) + "/" + fileName;
                    if (File.Exists(Server.MapPath(filePathName)))
                        imgInfo.imgPath = filePathName;
                    else
                        imgInfo.imgPath = consts.constNoImageFileName;
                }
                else
                    imgInfo.imgPath = consts.constNoImageFileName;
                imgInfo.logoText = sysLibs.SplitToMultiLine(dataTbl.Rows[idx]["name1"].ToString(),
                                   Common.systemSettings.sysNumberOfWordUnderLogo, Common.systemSettings.sysNumberOfLineUnderLogo);
                imgInfo.title = dataTbl.Rows[idx]["name1"].ToString();
                imgInfo.description = sysLibs.SplitToMultiLine(dataTbl.Rows[idx]["description"].ToString(),
                                            systemSettings.sysNumberOfWordInDescription, systemSettings.sysNumberOfLineInDescription);
                dataArr.Add(imgInfo);
            }
            //hoang them 1 bien portalId  de kiem tra portal techmart moi co javascript
            Tm_LogoList.Display(dataArr, Common.systemSettings.sysTypicalProductPerRow,myPortalId);
            return dataTbl.Rows.Count;
        }
        catch (Exception exc)
        {
            Exceptions.ProcessModuleLoadException(this, exc);
        }
        return 0;
    }
    

    private string GetFileName(int productId)
    {
        string fileName = "";
        try
        {
            string SQLCmd = " SELECT fileName FROM tm_product_file " +
                                " WHERE productId = " + productId + " AND fileType & " + sysLibs.turnBitOn(0, consts.constLOGOFileTypeBitPos) + " <> 0";
            SqlConnection conn = new SqlConnection(sysLibs.GetConnectionString);
            conn.Open();
            SqlCommand comm = new SqlCommand(SQLCmd, conn);
            fileName = comm.ExecuteScalar().ToString();
        }
        catch { }
        return fileName;
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}
